<?php

/**
 *   OPENSHOP 管理中心taobao 用户管理API
 * 
 *   @link        http://baison.com.cn
 *   @copyright   Baison, Inc.
 *   @package     OpenShop
 *   @version     $Id: api_tb_user.php,v 1.0 2009/10/10 08:36:58 created$
 *   @author      FillBag <fillbag@hotmail.com>
 */

define('IN_OS', true);

require(dirname(__FILE__) . '/includes/init.php'); 
require(ROOT_PATH . '/includes/modules/taobao.api/cls_api.php');  

	function AddItemGoods($result){    
		if (is_array($result['list'][0]   )) { 
			foreach ($result['list'] as $goods ){   
				InsertIntoTB_Goods($goods);  
			}   
		}else {    
			InsertIntoTB_Goods($result['list']);  
		}   
	}

	
	if($_REQUEST['status']=='' ){  
		$stuats = "ONSALE";	
	}
	else {
		$stuats=$_REQUEST['status'];
	}  
	
		
if ($_REQUEST['act'] == 'list'){	
	//检查权限 
	  $userParamArr = array(
				'fields' => 'approve_status,iid,num_iid,title,nick,type,cid,pic_path,num,props,valid_thru, 
							list_time,price,has_discount,has_invoice,has_warranty,has_showcase, 
							modified,delist_time,postage_id,seller_cids,outer_id',
				'page_size' => 20,
		     	'page_no' => ((isset($_GET['page']) && $_GET['page'] > 0) ? $_GET['page'] : 1)
	     	); 
	     	        
        if($stuats == 'INVENTORY'){  
         	 $result= TOP_ItemsInventoryGet($userParamArr,'item');
        }elseif ($stuats =='ALL'){ 
        	  $result= TOP_ItemsAllGet($userParamArr,'item');
        }else {   
        	  $result= TOP_ItemsOnsaleGet($userParamArr,'item');
        }        
		if(is_array($result) && is_array( $result['list']  ) ){
				$arr = array();
				foreach ($result['list'] as $row ){   
					$sql="select tb_item.id as tb_item_id,tb_item.detail_url, g.goods_name,g.goods_sn,g.goods_id from open_tb_item as tb_item
							left join open_goods AS g on g.goods_sn = tb_item.outer_id 
							WHERE iid='".$row['iid']."'";
		 			$os_order_row =$db->getRow($sql); //是否已经下载到本地 并关联商品。
		 			$row['detail_url']=$os_order_row['detail_url'] ; 
		 		    $row['tb_item_id']=$os_order_row['tb_item_id'] ;
					$row['goods_sn']=$os_order_row['goods_sn'] ; 
					$row['goods_id']=$os_order_row['goods_id'] ; 
					$row['goods_name']=$os_order_row['goods_name'] ;   
				    $arr[] = $row;
				}    
				if(!is_array($arr[0])) {
						$subresult[0] = $result['list'];
					} else {
						$subresult = $arr;
					}  			
		} 
		$result['list'] =$subresult;   
	    $smarty->assign('ur_here', '宝贝管理');
		$smarty->assign('goods_items', $result );  
		$smarty->assign('stuats', $stuats );   
        assign_query_info(); 
   		$smarty->display('api_tb_goods_list.tpl');
}elseif($_REQUEST['act'] == 'getpostages'){ //下载运费模板
	GetPostages(); 
}elseif($_REQUEST['act'] == 'uploadsku'){ // 上传库存。0库存则下架
	if( !$iid = $_REQUEST['iid'] )  return ;
    TOP_Item_Update_SKU( $iid ) ; 
}elseif($_REQUEST['act'] == 'delisting'){ 
	if( !$iid = $_REQUEST['iid'] )  return ;
	TOP_Item_UpdateDelisting( $iid ) ;      		//下架功能
}elseif($_REQUEST['act'] == 'listing'){  
	if( !$iid = $_REQUEST['iid'] )  return ; 
	TOP_Item_UpdateListing( $iid, $_REQUEST['num']);  //上架功能
}elseif($_REQUEST['act'] == 'get_item'){ 
	 $cid=$_REQUEST['gid'];    
	 $userParamArr = array(
				'fields' =>'iid,detail_url,num_iid,title,nick,type,cid,seller_cids,props,input_pids,input_str,desc,pic_path,num,
							valid_thru,list_time,delist_time,stuff_status,location,price,post_fee,express_fee,ems_fee,has_discount,
							freight_payer,has_invoice,has_warranty,has_showcase,modified,increment,auto_repost,approve_status,
							postage_id,product_id, auction_point,property_alias,itemimg, propimg, sku,outer_id,is_virtural,is_taobao,
							is_ex,video', 
				'page_size' => 20,
		     	'page_no' => ((isset($_GET['page']) && $_GET['page'] > 0) ? $_GET['page'] : 1)
	     	);    
	 if($cid=='ALL'){ // :得到当前会话用户的商品列表    
	 	$result =TOP_ItemsAllGet( $userParamArr ,'item' );
	 }elseif ($cid=='ONSALE' ) {	// :得到当前会话用户出售中的商品列表  
	    $result =TOP_ItemsOnsaleGet( $userParamArr ,'item' );       
	 }elseif ($cid=='INVENTORY') {  //:得到当前会话用户库存中的商品列表  
	    $result =TOP_ItemsInventoryGet( $userParamArr,'item' );       
	 }else {						// :得到单个商品信息  
	     $userParamArr['nick']= $_CFG['taobao_user'] ;
	     $userParamArr['num_iid']= $cid; 
	     $result =TOP_ItemGet( $userParamArr,'item' );         
	 }  
	 if( $result['recordCounts'] > count($result['list']) ) // 总数量 $result['list'] pagesize=20
		{
			for ($i=0;$i<=$result['recordCounts']/count($result['list']);$i++ )
			{  //页码($i+1)
				$page=$i+1;
			    $userParamArr = array(
								'fields' =>'iid,detail_url,num_iid,title,nick,type,cid,seller_cids,props,input_pids,input_str,desc,pic_path,num,
											valid_thru,list_time,delist_time,stuff_status,location,price,post_fee,express_fee,ems_fee,has_discount,
											freight_payer,has_invoice,has_warranty,has_showcase,modified,increment,auto_repost,approve_status,
											postage_id,product_id, auction_point,property_alias,itemimg, propimg, sku,outer_id,is_virtural,is_taobao,
											is_ex,video', 
								'page_size' => 20,
						     	'page_no' => $page
					     	);  
				if ($cid=='ONSALE' ){
				    $result1 =TOP_ItemsOnsaleGet( $userParamArr ,'item' );       
				}elseif ($cid=='INVENTORY'){
				    $result1 =TOP_ItemsInventoryGet( $userParamArr,'item' );  
				}elseif ($cid=='ALL'){
				  	$result1 =TOP_ItemsAllGet( $userParamArr ,'item' );
				} 
			 	AddItemGoods( $result1 );  //下载单页的goods	
			} 
		} elseif ( $result[recordCounts]>0 && $result[recordCounts]<20 ){ 
			    AddItemGoods( $result );   //不满一页的下载
		}else  {// 单个商品更新  
			 InsertIntoTB_Goods($result['item']);//下载单个的goods 
		} 
	echo ('下载成功! <a href="#" onclick="history.go(-1)">返回</a>');
}
?>